/*
Script: mooadapter.js
	match mootools1.2 & mootools1.11 syntax

Author: Ze
	Email:up2vs@yahoo.com
	QQ:11113533
	blog:hi.baidu.com/vsign
	
Code & Documentation:
	match mootools1.2 & mootools1.11 syntax
*/
Window.implement({
		ie:Browser.Engine.trident,
		ie6:Browser.Engine.trident4,
		ie7:Browser.Engine.trident5,
		gecko:Browser.Engine.gecko,
		webkit:Browser.Engine.webkit,
		webkit419:Browser.Engine.webkit419,
		webkit420:Browser.Engine.webkit420,
		opera:Browser.Engine.presto,
		xpath:Browser.Features.xpath
		});
		
Object.toQueryString=function(source){
  return Hash.toQueryString(new Hash(source));
}		
		
/*Class*/
    Class.empty=$empty;
    Class.prototype.extend=function(properties){
	    properties=$merge(properties,{Extends:this});
		return new Class(properties);
	};


/*Element*/
Window.implement({
   $E:function(selector,scope){
      return ($(scope)||document).getElement(selector);
   },
   $ES:function(selector,scope){
      return ($(scope)||document).getElements(selector);
   }
});
Element.implement({
   setHTML:function(){
     return this.set('html',Array.flatten($A(arguments)).join('\n'));
   },
   setText:function(text){
     return this.set('text',text);
   },
   getText:function(){
     return this.get('text');
   },
   getHTML:function(){
    return this.get('html');
   },
    setOpacity:function(value){
		return this.set('opacity', value, false);
   },
   getTag:function(){
   return this.tagName.toLowerCase();
   },
   replaceWith:function(el){
        var newEL=$(el, true);
		var oEL=$(this);
		this.parentNode.replaceChild(newEL, oEL);
		return newEL;
    },
	getValue: function(){
		switch(this.getTag()){
			case 'select':
				var values = [];
				for(i=0,L=this.options.length;i<L;i++){
				if (this.options[i].selected) values.push($pick(this.options[i].value, this.options[i].text));
				}
				return (this.multiple) ? values : values[0];
			case 'input': if (!(this.checked && ['checkbox', 'radio'].contains(this.type)) && !['hidden', 'text', 'password'].contains(this.type)) break;
			case 'textarea': return this.value;
		}
		return false;
	},
	getFormElements: function(){
		return $$(this.getElementsByTagName('input'), this.getElementsByTagName('select'), this.getElementsByTagName('textarea'))||[];
	},
	remove:function(){
	  return this.dispose();
	}
});
/*Json */
var Json={
   'toString':function(json){
     return JSON.encode(json)||"";
   },
   'evaluate':function(json,secure){
     return JSON.decode(json,secure)||{};
   }
}
Json.Remote=new Class({
      Extends:Request.JSON,
	  initialize:function(url,options){
	     this.parent($extend(options,{'url':url}));
	  }
});

/*Cookie */
Cookie.set=Cookie.write;
Cookie.get=Cookie.read;
Cookie.remove=Cookie.dispose;

/*XHR& AJAX */
var XHR=new Class({
    Extends:Request,
	send:function(url,options){
		var type = $type(options);
		if (type == 'string' || type == 'element') options = {data: options};
		options = $extend({url: url}, options);
   	   this.parent(options);
	}
});

var Ajax=new Class({
     Extends:XHR,
	 options: {
		data: null,
		update: false,
		onComplete: Class.empty,
		evalScripts: true,
		evalResponse: false
	},
	 initialize:function(url,options){
		this.parent(options);
		this.url=url;
		this.transport=this.xhr;
		return this;
	 },
	 processHTML: function(text){
		var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
		text = (match) ? match[1] : text;
		var container = new Element('div');
		
		return $try(function(){
			var root = '<root>' + text + '</root>', doc;
			if (Browser.Engine.trident){
				doc = new ActiveXObject('Microsoft.XMLDOM');
				doc.async = false;
				doc.loadXML(root);
			} else {
				doc = new DOMParser().parseFromString(root, 'text/xml');
			}
			root = doc.getElementsByTagName('root')[0];
			for (var i = 0, k = root.childNodes.length; i < k; i++){
				var child = Element.clone(root.childNodes[i], true, true);
				if (child) container.grab(child);
			}
			return container;
		}) || container.set('html', text);
	},
	success: function(text,xml){
		var options = this.options, response = this.response;
		
		response.html = text.stripScripts(function(script){
			response.javascript = script;
		});
		
		var temp = this.processHTML(response.html);
		
		response.tree = temp.childNodes;
		response.elements = temp.getElements('*');
		
		if (options.filter) response.tree = response.elements.filter(options.filter);
		if (options.update){
		   if(window.webkit){
		    $(options.update).empty().setHTML(response.html);
		   }else{
		     $(options.update).empty().adopt(response.tree);
		   }
		} 
		if (options.evalScripts) $exec(response.javascript);
	    
		this.onSuccess(response.html,response.tree, response.elements,response.javascript);
	},
	onFailure:function(){
	  this.fireEvent('failure', this.xhr);
	},
	 request:function(data){
	   return this.send(this.url,data);
	 }
});

Element.implement({

	send: function(options){
	    var type=$type(options);
		var sender = this.get('send');
		if(type=='object'){
			new Ajax(this.action||options.url,options).request(this);
			return this;
		} else{
		sender.send({data: this, url: options || sender.options.url});
		return this;
		}
	}

});



/*FX */
Fx.implement({
 stop:function(){
   return this.cancel();
 }
});
Fx.Style=new Class({
   Extends:Fx.Tween,
   initialize:function(el,property,options){
      this._property=property;
	  this.parent(el,options);
   },
   set:function(v){
     return this.parent(this._property,v);
   },
   start:function(f,t){
     return this.parent(this._property,f,t);
   }
});
Fx.Styles=new Class({
   Extends:Fx.Morph
});

Fx.Scroll.implement({
  scrollTo:function(x,y,effect){
     if(effect)return this.start(x,y);
	 return this.set(x,y);
  } 
});
Element.implement({
   effect:function(p,o){
   return new Fx.Style(this,p,o);
   },
   effects:function(o){
   return new Fx.Styles(this,o);
   }
});

/*Abstract*/
var Abstract = function(obj){
	obj = obj || {};
	obj.extend = $extend;
	return obj;
};

/*getSize */
(function(){
Element.implement({
	getSize: function(){
		if (isBody(this)) return this.getWindow().getSize();
		return {
		x: this.offsetWidth,
		y: this.offsetHeight,
		'size':{x:this.offsetWidth,y:this.offsetHeight},
		'scroll':{x: this.scrollLeft, y: this.scrollTop},
		'scrollSize':{x: this.scrollWidth, y: this.scrollHeight}
		};
	}
});

Native.implement([Document, Window], {
	getSize: function(){
		var win = this.getWindow();
		var doc = getCompatElement(this);
		if (Browser.Engine.presto || Browser.Engine.webkit)
		return {
		        x: win.innerWidth, 
		        y: win.innerHeight,
				'size':{'x':win.innerWidth,'y':win.innerHeight},
		        'scroll':{x: win.pageXOffset,y: win.pageYOffset},
			        'scrollSize':{
					x: Math.max(doc.scrollWidth, win.innerWidth),
					y: Math.max(doc.scrollHeight, win.innerHeight)
					}
			   };
		return {
		x: doc.clientWidth,
		y: doc.clientHeight,
		'size':{'x':doc.clientWidth,'y':doc.clientHeight},
		'scroll':{x: win.pageXOffset || doc.scrollLeft, y: win.pageYOffset || doc.scrollTop},
		  'scrollSize':{
					x: Math.max(doc.scrollWidth, win.innerWidth),
					y: Math.max(doc.scrollHeight, win.innerHeight)
					}
		};
	}
});

// private methods
function isBody(element){
	return (/^(?:body|html)$/i).test(element.tagName);
};

function getCompatElement(element){
	var doc = element.getDocument();
	return (!doc.compatMode || doc.compatMode == 'CSS1Compat') ? doc.html : doc.body;
};

})();


/*Array */

Array.implement({
  copy:function(){
     return $A(this);
  }
});
Array.alias('erase','remove');

/*Hash */
Hash.alias('erase','remove');
Hash.alias('getKeys','keys');
Hash.alias('getValues','values');
Hash.alias('has','hasKey');
Hash.implement({
  merge:function(){
   return  $merge.apply(null,[this].include(arguments));
  }
});



/*Drag.base */
Drag.implement({
  options: {/*
		onBeforeStart: $empty,
		onStart: $empty,
		onDrag: $empty,
		onCancel: $empty,
		onComplete: $empty,*/
		snap: 0,
		unit: 'px',
		grid: false,
		style: true,
		limit: false,
		handle: false,
		invert: false,
		preventDefault: true,
		modifiers: {x: 'left', y: 'top'}
	}
});
Drag.Base=Drag;


/*Extends*/
[Element,Number,String].each(function(o){
    o.extend=o.implement;
 })
 
 
 /*bindwithEventL..*/
 
 Function.implement({
  bindAsEventListener: function(bind, args){
		return this.create({'bind': bind, 'event': true, 'arguments': args});
	}
 });
